package com.jdew.thkj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.OrderBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<OrderBean.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private OnItemClickListener mItemClickListener;

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public OrderAdapter(Context context, List<OrderBean.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setDataList(List<OrderBean.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setDataAllList(List<OrderBean.DataBean> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = layoutInflater.inflate(R.layout.tb_order_item, parent, false);
        itemView.setOnClickListener(this);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myviewHolder = (MyViewHolder) holder;
        myviewHolder.BindView(holder, position);
        myviewHolder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.pic_url)
        ImageView picUrl;
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.shop)
        TextView shop;
        @Bind(R.id.status)
        TextView state;
        @Bind(R.id.order_no)
        TextView orderNo;
        @Bind(R.id.fuzhi)
        ImageView fuzhi;
        @Bind(R.id.pay)
        TextView pay;
        @Bind(R.id.earn)
        TextView earn;
        @Bind(R.id.begin_time)
        TextView beginTime;
        @Bind(R.id.end_time)
        TextView endTime;
        @Bind(R.id.order_sources)
        TextView order_sources;

        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
            DecimalFormat df = new DecimalFormat("0.00");
            ((MyViewHolder) holder).title.setText(dataList.get(position).getGoods_name());
            ((MyViewHolder) holder).shop.setText(dataList.get(position).getShop());
             if(dataList.get(position).getOrder_status().equals("订单结算")){
                 ((MyViewHolder) holder).state.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
                 ((MyViewHolder) holder).state.setText("已结算");
             }else if(dataList.get(position).getOrder_status().equals("订单付款") || dataList.get(position).getOrder_status().equals("订单成功")){
                 ((MyViewHolder) holder).state.setBackgroundColor(context.getResources().getColor(R.color.red));
                 ((MyViewHolder) holder).state.setText("已付款");
             }else if(dataList.get(position).getOrder_status().equals("订单失效")){
                 ((MyViewHolder) holder).state.setBackgroundColor(context.getResources().getColor(R.color.color_999999));
                 ((MyViewHolder) holder).state.setText("已失效");
             }
            ((MyViewHolder) holder).title.setText(dataList.get(position).getGoods_name());
            ((MyViewHolder) holder).order_sources.setText(dataList.get(position).getSysID());
            ((MyViewHolder) holder).orderNo.setText(dataList.get(position).getOrder_sn());
            ((MyViewHolder) holder).pay.setText("¥ "+dataList.get(position).getBalance_amount());
            ((MyViewHolder) holder).earn.setText("¥ "+dataList.get(position).getEffect_prediction());
            ((MyViewHolder) holder).beginTime.setText(dataList.get(position).getCreate_time().replace("T"," ")+"  创建");
            if(dataList.get(position).getBalance_time().equals("2000-01-01T00:00:00") || dataList.get(position).getBalance_time().equals("1900-01-01T00:00:00")){
                ((MyViewHolder) holder).endTime.setVisibility(View.GONE);
            }else{
                ((MyViewHolder) holder).endTime.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).endTime.setText(dataList.get(position).getBalance_time().replace("T"," ")+"  结算");
            }

            RequestOptions options = new RequestOptions().override(310, 310).placeholder(R.mipmap.ploder).diskCacheStrategy(DiskCacheStrategy.NONE);
            if(null==dataList.get(position).getAdv_name()|| dataList.get(position).getAdv_name().equals("")){
                Glide.with(context).load(R.mipmap.xiajia).apply(options).into(picUrl);
            }else{
                Glide.with(context).load(dataList.get(position).getAdv_name()).apply(options).into(picUrl);
            }
            ((MyViewHolder) holder).fuzhi.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.fuzhi: {
                    RegexValidateUtils.copyText(context, orderNo.getText().toString());
                    ToastUtils.getInstance(context).showMessage("复制成功");
                }
                break;
                default:
                    break;
            }
        }
    }
}

